package org.arnetminer.personalized.dao;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BaseDAOImpl extends HibernateDaoSupport implements BaseDAO {
	
	public void initDao() {
		//do nothing
	}
    
    public void save(Object transientInstance) {
        try {
        	getHibernateTemplate().saveOrUpdate(transientInstance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
	public void delete(Object persistentInstance) {
        try {
            getHibernateTemplate().delete(persistentInstance);
        } catch (RuntimeException re) {
        	re.printStackTrace();
            throw re;
        }
    }
    
    public List findByExample(Object instance) {
        try {
            List results = getHibernateTemplate().findByExample(instance);
            return results;
        } catch (RuntimeException re) {
            throw re;
        }
    }    

    public Object merge(Object detachedInstance) {
        try {
        	Object result = (Object) getHibernateTemplate()
                    .merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public void attachDirty(Object instance) {
        try {
            getHibernateTemplate().saveOrUpdate(instance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public void attachClean(Object instance) {
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
